<?php
/**
 * 定量计算详情控制器
 */

namespace app\school\controller;

use app\common\model\Type;
use app\common\validate\ComputeDetailValidate;
use app\school\exception\ApiServiceException;
use app\school\service\ComputeDetailService;
use hg\apidoc\annotation as Apidoc;
use think\response\Json;

/**
 * @Apidoc\Title("未验收订单")
 * @Apidoc\Group("index")
 */
class ComputeDetailController extends ApiBaseController {
	/**
	 * @Apidoc\Title("未验收订单列表")
	 * @Apidoc\Desc("列表")
	 * @Apidoc\Method("GET")
	 * @Apidoc\Url("/school/compute_detail/index")
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function index(ComputeDetailService $service): Json {
		try {
			$this->param['status'] = 0;
			$this->param['school_id'] = $this->uid;
			if (isset($this->param['type']) && $this->param['type'] != '') {
				$type = Type::where('name', 'like', '%' . $this->param['type'] . '%')->where('tag', 1)->findOrEmpty();
				if (!$type->isEmpty()) {
					$this->param['type_id'] = $type['id'];
				} else {
					return api_error('未查询到');
				}

			}

			$data = $service->getList($this->param, $this->page, $this->limit);
			$result = [
				'compute_detail' => $data,
			];

			return api_success($result);
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 添加
	 * @param ComputeDetailValidate $validate
	 * @param ComputeDetailService $service
	 * @return Json
	 */
	public function add(ComputeDetailValidate $validate, ComputeDetailService $service): Json {
		$check = $validate->scene('api_add')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		$result = $service->createData($this->param);

		return $result ? api_success() : api_error();
	}

	/**
	 * 详情
	 * @param ComputeDetailValidate $validate
	 * @param ComputeDetailService $service
	 * @return Json
	 */
	public function info(ComputeDetailValidate $validate, ComputeDetailService $service): Json {
		$check = $validate->scene('api_info')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {

			$result = $service->getDataInfo($this->id);
			return api_success([
				'compute_detail' => $result,
			]);

		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 修改
	 * @param ComputeDetailService $service
	 * @param ComputeDetailValidate $validate
	 * @return Json
	 */
	public function edit(ComputeDetailService $service, ComputeDetailValidate $validate): Json {
		$check = $validate->scene('api_edit')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->updateData($this->id, $this->param);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 删除
	 * @param ComputeDetailService $service
	 * @param ComputeDetailValidate $validate
	 * @return Json
	 */
	public function del(ComputeDetailService $service, ComputeDetailValidate $validate): Json {
		$check = $validate->scene('api_del')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->deleteData($this->id);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

}
